package com.gears_software.sxdc.dccx.ui.view;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.GravityEnum;
import com.afollestad.materialdialogs.MaterialDialog;
import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.application.DCCXApplication_;
import com.gears_software.sxdc.dccx.ui.activity.ImmediatelyReserveActivity_;
import com.gears_software.sxdc.dccx.ui.activity.MainActivity;
import com.sdsmdg.tastytoast.TastyToast;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionListener;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RationaleListener;

import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;

import java.util.List;

/**
 * Created by chezhenzhen on 2017/8/1.
 * 乘客行程itemview
 */

@EViewGroup(R.layout.item_view_passenger_travel)
public class PassengerTravelItemView extends LinearLayout {

    @App
    DCCXApplication app;
    @ViewById
    TextView txt_tel;
    @ViewById
    TextView txt_triptime_icon;
    @ViewById
    TextView txt_jiantou_icon;
    @ViewById
    TextView txt_yuding_icon;
    @ViewById
    TextView txt_xianlu_icon;
    Context context;

    public PassengerTravelItemView(Context context) {
        super(context);
        this.context = context;
    }

    public void bind() {
        txt_tel.setTypeface(app.iconfont);
        txt_triptime_icon.setTypeface(app.iconfont);
        txt_jiantou_icon.setTypeface(app.iconfont);
        txt_yuding_icon.setTypeface(app.iconfont);
        txt_xianlu_icon.setTypeface(app.iconfont);
    }

    @Click
    void txt_tel() {
        AndPermission.with((MainActivity) context)
                .requestCode(100)
                .permission(Manifest.permission.CALL_PHONE)
                .callback(listener)
                .rationale(new RationaleListener() {
                    @Override
                    public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
                        // 这里的对话框可以自定义，只要调用rationale.resume()就可以继续申请。
                        AndPermission.rationaleDialog(context, rationale).
                                show();
                    }
                })
                .start();
    }

    private PermissionListener listener = new PermissionListener() {
        @Override
        public void onSucceed(int requestCode, List<String> grantedPermissions) {
            // 权限申请成功回调。
            if (requestCode == 100) {
                //打电话
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:" + "15535521818"));
                context.startActivity(intent);
            }
        }

        @Override
        public void onFailed(int requestCode, List<String> deniedPermissions) {
            // 权限申请失败回调。
            if (requestCode == 100) {
                TastyToast.makeText(context, "获取电话权限失败", TastyToast.LENGTH_SHORT, TastyToast.DEFAULT);

            }
        }
    };

    @Click
    void ly_yuding() {
        Intent intent = new Intent(context, ImmediatelyReserveActivity_.class);
        context.startActivity(intent);
    }

    @Click
    void ly_xianlu() {
        new MaterialDialog.Builder(context)
                .title("途径路线")
                .titleGravity(GravityEnum.CENTER)
                .titleColorRes(R.color.colorFontBigGray)
                .content("文化苑东社、西中环、南中环、小马、龙城大街")
                .contentColorRes(R.color.colorFontGray)
                .positiveText("确定")
                .positiveColor(ContextCompat.getColor(context, R.color.colorFontBlue))
                .buttonsGravity(GravityEnum.CENTER)
                .show();
    }
}
